package com.javasoft.controller.ZSW;

import com.javasoft.entity.XGX.Question;
import com.javasoft.service.LCB.RemoveQueService;
import com.javasoft.service.XGX.impl.QuestionImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/plentyadd")
public class PlentyAddController {
    @Autowired
    private RemoveQueService removeQueService;

    @PostMapping("/add")
    public List<Long> add(@RequestBody List<Question> newQuestionsList) {
        List<Long> resultIds = new ArrayList<>();
        List<Question> questionsToInsert = new ArrayList<>();

        // 先遍历一遍，收集所有非空且描述不为空的问题，用于后续去重插入
        for (Question question : newQuestionsList) {
            if (question!= null && question.getDes()!= null &&!question.getDes().trim().isEmpty()) {
                questionsToInsert.add(question);
            }
        }

        // 用于记录已经存在的问题描述，避免重复去重检查
        List<String> existingDescriptions = new ArrayList<>();

        // 再次遍历，进行去重检查和插入操作
        for (Question question : questionsToInsert) {
            if (!existingDescriptions.contains(question.getDes())) {
                // 检查数据库中是否已经存在相同的内容
                if (removeQueService.addQuestionWithDuplicateCheck(question)) {
                    resultIds.add(question.getId());
                    existingDescriptions.add(question.getDes());
                }
            }
        }

        return resultIds;
    }

}
